<template>
    <el-breadcrumb separator-icon="ArrowRight" style="margin-bottom: 50px;">
        <el-breadcrumb-item :to="{ path: '/home' }">主页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/home/projectList' }">项目列表</el-breadcrumb-item>
        <el-breadcrumb-item>编辑页</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form ref="editRuleForm" label-width="100px" :model="form_data.field" :rules="form_data.form_rules">
        <el-form-item label="项目名称：" prop="name">
            <el-input v-model="form_data.field.name" placeholder="请编辑项目名称"></el-input>
        </el-form-item>
        <el-form-item label="项目版本：" prop="version">
            <el-input v-model="form_data.field.version" placeholder="请编辑项目版本"></el-input>
        </el-form-item>
        <el-form-item label="项目详情：" prop="description">
            <el-input v-model="form_data.field.description" type="textarea" placeholder="请编辑项目详情"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="success" @click="submitForm('addRuleForm')">提交</el-button>
            <el-button type="danger" @click="resetForm">重置</el-button>
        </el-form-item>
    </el-form>s
</template>

<script>
import { reactive, getCurrentInstance, onMounted } from 'vue';
import { GetDetailed ,InfoEdit } from "../../api/http.js"
import { useRouter, useRoute } from "vue-router";
export default {
    name: "ProjectEdit",
    setup(props) {

        //form表单

        const form_data = reactive({
            field: {
                name: "",
                version: "",
                description: "",
            },
            form_rules: {
                name: [{ required: true, message: "项目名称不能为空", trigger: 'blur' }],
                version: [{ required: true, message: "项目版本不能为空", trigger: 'blur' }],
                description: [{ required: true, message: "项目详情不能为空", trigger: 'blur' }],
            }
        })


        const { proxy } = getCurrentInstance()
        const { query } = useRoute();
        const { go } = useRouter();
        const data = reactive({
            userid: query.userid,
            projectid: query.projectid,
        })
        // 获取详情
        const handlerGetDetailed = () => {
            GetDetailed(data).then(response => {
                console.log(data)
                //console.log(form_data.field)
                form_data.field = response.data.data
                console.log("获取编辑行内容")
                console.log(form_data.field)

            })
        }
        
        const submitForm = () => {
            const request_data = JSON.parse(JSON.stringify(form_data.field));
            //console.log("提交")
            //console.log(request_data)
            proxy.$refs.editRuleForm.validate((valid) => {
                if (valid) {
                    InfoEdit(request_data).then(response => {
                        // 弹窗提示
                        proxy.$message.success(response.data.message);
                        // 重置表单
                        go(-1);
                    })
                    console.log('success submit!!')
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        }

        // 重置
        const resetForm = () => {
            proxy.$refs.editRuleForm.resetFields();
        }

        onMounted(() => {
            handlerGetDetailed();
        })

        return {
            data,
            form_data,
            submitForm,
            resetForm,
        }
    }
}
</script>

<style scoped></style>  